﻿using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

/*************************************************************************************************************************************
 * Created by:     Charles Turiño
 * Date created:   04/23/2012
 * Modified by: 
 * Date modified: 
 * Reason:         
 * ULTI-00000 JIRA Description. This testsuite will validate that access rights for the Business Rules -> Tax Summary page
 * *************************************************************************************************************************************/

namespace Echo.Automation.Compliance.BusinessRules.Taxes
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Charles Turiño", "charles_turino@ultimatesofteware.com")]
    [Category("Business Rules")]
    [Category("Taxes")]
    [Category("Access Rights")]

    public class TaxesAccessRights : MbUnitAutomatedTest
    {
        UltiPro UltiPro;
        UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
        }

        [SetUp]
        public void Setup()
        {
            // Need to setup some data in the DB to create access rights 
            Database.Company.AddRollBack("RbsBusRuleRole", "rbrBusinessRule = 'TAXCODE' and rbrRoleID = '14'");
            Database.Company.AddRollBack("RbsBusRuleRole", "rbrBusinessRule = 'TAXCODE' and rbrRoleID = '121'");
            Database.Company.AddRollBack("RbsBusRuleRole", "rbrBusinessRule = 'TAXCODE' and rbrRoleID = '99'");
            Database.Company.AddRollBack("RbsBusRuleRole", "rbrBusinessRule = 'TAXCODE' and rbrRoleID = '130'");

            // Insert security record for role SNWFSA to set Taxes page to View ON and Edit OFF (roleID = 14)
            Database.Company.Execute("Insert into RbsBusRuleRole (rbrBusinessRule, RbrRightsMask, rbrRoleID, rbrType) Values ('TAXCODE',0x0000000002,14,'B')");
            // Insert security record for role SUEPRGTWYSYSADM to set Taxes page to View/Edit ON (roleID = 121)
            Database.Company.Execute("Insert into RbsBusRuleRole (rbrBusinessRule, RbrRightsMask, rbrRoleID, rbrType) Values ('TAXCODE',0x0000000006,121,'B')");
            // Insert security record for role SNPRSYS to set Taxes page to View/Edit ON (roleID = 99)
            Database.Company.Execute("Insert into RbsBusRuleRole (rbrBusinessRule, RbrRightsMask, rbrRoleID, rbrType) Values ('TAXCODE',0x0000000006,99,'B')");
            // Insert security record for role SUESYSADMEEADM to set Taxes page to View ON and Edit OFF (roleID = 130)
            Database.Company.Execute("Insert into RbsBusRuleRole (rbrBusinessRule, RbrRightsMask, rbrRoleID, rbrType) Values('TAXCODE',0x0000000002,130,'B')");
        }

        /* This testcase will test that the Business Rules -> taxes page does not display
         * for users who do not have acces rights.
         * 
         * User: PerryE/password
         * PerryE|SUENOPR|Sys Admin - cannot see page|rights to BR, but NO RIGHTS to Taxes BR page|
         * */
        [Test]
        public void ValidateRoleThatDoesNotHavingAccessRightsToTaxPage()
        {
            UltiPro.Pages.Login.LoginAs("PerryE", "password", Config.LanguageCode);
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Is(grid => grid.GridMessage, "innerHtml", "No records found");
            UltiPro.Macros.Logout.Of_UltiPro();
        }

        /* This testcase will test that the Business Rules -> taxes page displays
         * for users who do have view only acces rights.
         * 
         * User: CarterB/password
         * |CarterB|SNWFSA|Sys Admin - two role types in one role - has view only - but NO EDIT rights - rights to
         *  BR with rights to Taxes BR page, with Company qualifiers for a US and CAN company|should see only US company tax codes|
         * */
        [Test]
        public void ValidateRoleHavingTwoRolTypes()
        {
            string recordCount;
            UltiPro.Pages.Login.LoginAs("CarterB", "password", Config.LanguageCode);
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(columnHeader => columnHeader.BusinessRulesLink, "Taxes").Click(link => link.BusinessRulesLink);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Click(searchButton => searchButton.FilterButton);
            recordCount = Database.Company.GetValue("select count(*) as CarterBRecordCount from taxcode where ctcState in (select code from states) AND ctcCoID = '7WK1U' AND CtcCountryCode = 'USA' and CtcTypeOfTax in ('SIT', 'SUI', 'LIT', 'SDI') and getdate() > ctcEffectiveDate and getdate() < ctcEffectiveStopDate");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Like(grid => grid.TotalRecordsRetrieved, "innerHtml", recordCount);
            UltiPro.Macros.Logout.Of_UltiPro();
        }

        /*This testcase will test that a user with one or more roles that include the System Admin
        * role type with different company qualifiers, he can see tax codes for all companies he is
        * qualified to see, based on the System Admin role selected on the Business Rules Summary 
        * page, if any.
        * 
        * User: JenkinsB/password
        * |JenkinsB|SUEPRGTWYSYSADM + SNMGR|Two roles - Sys Admin exists in only 
        * one - with Mgr have numerous companies and Sys Admin only having one|should see 
        * tax codes for the company qualified for under Sys Admin|
        * */
        [Test]
        public void ValidateTwoRolesWithSysAdminOnlyInOne()
        {
            string recordCount;
            UltiPro.Pages.Login.LoginAs("JenkinsB", "password", Config.LanguageCode);
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(columnHeader => columnHeader.BusinessRulesLink, "Taxes").Click(link => link.BusinessRulesLink);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Click(searchButton => searchButton.FilterButton);
            recordCount = Database.Company.GetValue("select count(*) as JenkinsBRecordCount from taxcode where ctcState in (select code from states) AND ctcCoID = 'BVEBW' AND CtcCountryCode = 'USA' and CtcTypeOfTax in ('SIT', 'SUI', 'LIT', 'SDI') and getdate() > ctcEffectiveDate and getdate() < ctcEffectiveStopDate");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Like(grid => grid.TotalRecordsRetrieved, "innerHtml", recordCount);
            UltiPro.Macros.Logout.Of_UltiPro();
        }

        /*This testcase will test that a user with one or more roles that include the System Admin 
         * role type with different company qualifiers, he can see tax codes for all companies he 
         * is qualified to see, based on the System Admin role selected on the Business Rules 
         * Summary page, if any.
         * 
         * User: BakerB2/password
         * |BakerB2|SNPRSYS + SUESYSADMEEADM|Two roles - Sys Admin exists in both - both roles have
         * access to Taxes BR page, with Company qualifiers that have differences and similarities
         * |should see tax codes for all the qualified companies for combined roles|
         * */
        [Test]
        public void ValidateTwoRolesWithSysAdminInBoth()
        {
            string recordCount;
            UltiPro.Pages.Login.LoginAs("BakerB2", "password", Config.LanguageCode);
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(columnHeader => columnHeader.BusinessRulesLink, "Taxes").Click(link => link.BusinessRulesLink);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Click(searchButton => searchButton.FilterButton);
            recordCount = Database.Company.GetValue("select count(*) as BakerB2RecordCount from taxcode where ctcState in (select code from states) AND ctcCoID in ('BVEBW', 'FGV46')  AND CtcCountryCode = 'USA' and CtcTypeOfTax in ('SIT', 'SUI', 'LIT', 'SDI') and getdate() > ctcEffectiveDate and getdate() < ctcEffectiveStopDate");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Like(grid => grid.TotalRecordsRetrieved, "innerHtml", recordCount);

            UltiPro.Pages.BusinessRuleDetail.ButtonBar.Click(backButton => backButton.Back);
            UltiPro.Pages.BusinessRulesSummary.Set(role => role.Role, "SUESYSADMEEADM");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(columnHeader => columnHeader.BusinessRulesLink, "Taxes").Click(link => link.BusinessRulesLink);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Click(searchButton => searchButton.FilterButton);
            recordCount = Database.Company.GetValue("select count(*) as BakerB2RecordCount from taxcode where ctcState in (select code from states) AND ctcCoID in ('7WK1U', 'BVEBW')  AND CtcCountryCode = 'USA' and CtcTypeOfTax in ('SIT', 'SUI', 'LIT', 'SDI') and getdate() > ctcEffectiveDate and getdate() < ctcEffectiveStopDate");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Like(grid => grid.TotalRecordsRetrieved, "innerHtml", recordCount);
            
            UltiPro.Macros.Logout.Of_UltiPro();
        }

        /*This testcase will test that a user with one or more roles that include the System Admin
         * role type with different company qualifiers, he can see tax codes for all companies he is
         * qualified to see, based on the System Admin role selected on the Business Rules Summary 
         * page, if any.
         * 
         * User: BellT/password
         *|BellT|SUEPRGTWYSYSADM|role includes Sys Admin + PR Admin - rights to BR with rights to Taxes
         * BR page, with no Company qualifiers, only has Pay Group qualifiers|should see tax codes for
         * NO companies|
         * */
        [Test]
        public void ValidateRoleThatIncludesSysAdminAndPRAdmin()
        {
            UltiPro.Pages.Login.LoginAs("BellT", "password", Config.LanguageCode);
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(columnHeader => columnHeader.BusinessRulesLink, "Taxes").Click(link => link.BusinessRulesLink);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Click(searchButton => searchButton.FilterButton);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Is(x => x.GridMessage, "innerHtml", "No records found");
            UltiPro.Macros.Logout.Of_UltiPro();
        }

        /*This testcase will test that a user with one or more roles that include the System Admin
         * role type with different company qualifiers, he can see tax codes for all companies he is
         * qualified to see, based on the System Admin role selected on the Business Rules Summary 
         * page, if any.
         * 
         * User: SusanN/password
         * |SusanN|Site Administrator|can see all pages|should see ALL tax codes|
         * */
        [Test]
        public void ValidateSiteAdministratorRole()
        {
            string recordCount;
            UltiPro.Pages.Login.LoginAs("SusanN", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
            UltiPro.Menu.SystemConfiguration.Click(link => link.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(search => search.BusinessRulesNameFilter, FilterType.Is, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(columnHeader => columnHeader.BusinessRulesLink, "Taxes").Click(link => link.BusinessRulesLink);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Click(searchButton => searchButton.FilterButton);
            recordCount = Database.Company.GetValue("select count(*) as SiteAdminRecordCount from taxcode where ctcState in (select code from states) AND ctccoid in (select cmpcoid from company) AND CtcCountryCode = 'USA' and CtcTypeOfTax in ('SIT', 'SUI', 'LIT', 'SDI') and getdate() > ctcEffectiveDate and getdate() < ctcEffectiveStopDate");
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Like(grid => grid.TotalRecordsRetrieved, "innerHtml", "113632");
            UltiPro.Macros.Logout.Of_UltiPro();
        }

        [TearDown]
        public void TearDown()
        {
            Database.Company.RollBackDatabase();
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit();
        }
    }
}
